package com.hdoit.ares.logical.cust.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.brokerage.model.BrokerageRecord;
import com.hdoit.ares.logical.brokerage.model.CarBrokerageRecord;
import com.hdoit.ares.logical.cust.dao.ICustomerDao;
import com.hdoit.ares.logical.cust.mapper.CustomerMapper;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.cust.CustomerColumn;
import com.hdoit.ares.logical.viewmodel.cust.CustomerSearch;
import com.hdoit.ares.logical.viewmodel.cust.CustomerStatistic;
import com.hdoit.ares.logical.viewmodel.cust.CustomerTree;
import com.hdoit.ares.logical.viewmodel.cust.TeamShareCustomer;
import com.hdoit.ares.selfcommon.enums.cust.CustomerLevel;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DESedeCoder;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      chenghuan
 * @email       
 * @createtime  2017年10月26日 16:25:53
 * @comment
 */
@Component
public class CustomerDaoImpl extends SuperDao<Customer> implements ICustomerDao{
    
	private CustomerMapper customerMapper;
	
	public CustomerMapper getCustomerMapper() {
		return customerMapper;
	}
    
	@Resource(name = "customerMapper")
	public void setCustomerMapper(CustomerMapper customerMapper) {
		this.customerMapper = customerMapper;
	}

	@Override
	public Customer queryModelByUserIdentity(int userIdentity) {	
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(Customer.class)
						.andEq("userIdentity",String.valueOf(userIdentity))
						.addOrder("userIdentity", OrderEnum.desc);
		
		return queryModel(finderExpression);
	}

	@Override
	public String queryIdByUserIdentity(int userIdentity) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("id")
						.from(Customer.class)
						.andEq("userIdentity",String.valueOf(userIdentity));
		
		return queryScalar(finderExpression);
	}

	@Override
	public int queryMaxUserIdentity() {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("max(userIdentity) as m")
						.from(Customer.class);
		String scalar=queryScalar(finderExpression);
		if(StringUtils.isEmpty(scalar)){
			return 0;
		}
		return CommonUtils.getNumber(scalar);
	}

	@Override
	public Customer queryModelByOpenId(String openId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(Customer.class)
						.andEq("openId",openId);
		
		return queryModel(finderExpression);
	}

	@Override
	public int queryInviteSum(String id) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(id)")
						.from(Customer.class)
						.andEq("refereeId", id);
		
		return queryCount(finderExpression);
	}

	@Override
	public boolean updateRegister(Customer customer) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.update(Customer.class)
						.set("phone=#{phone}")	
				
						.andWhere("id=#{id}")
						.setParam("phone", customer.getPhone())		
						.setParam("id", customer.getId());
		return update(finderExpression) > 0;
	}
	
	@Override
	public List<TeamShareCustomer> queryOneTeamShare(String id) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath","count(c2.id) as personCount")
						.from(Customer.class,"c1")
						.leftJoin(Customer.class,"c2").on("c2.refereeId=c1.id")
						.andWhere("c1.refereeId=#{refereeId}")
						.setParam("refereeId", id)
						.addGroups("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath");
		
		return queryList(finderExpression, TeamShareCustomer.class);
	}

	@Override
	public List<TeamShareCustomer> queryTwoTeamShare(String id) {		
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("id")
						.from(Customer.class)
						.andWhere("refereeId=#{refereeId}");
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath","count(c2.id) as personCount")
						.from(Customer.class,"c1")
						.leftJoin(Customer.class,"c2").on("c2.refereeId=c1.id")
						.andWhere("c1.refereeId in("+oneChildIdFinder.toSql()+")")
						.setParam("refereeId", id)
						.addGroups("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath");
		
		return queryList(finderExpression, TeamShareCustomer.class);
	}

	@Override
	public List<Customer> queryCurrentChildCustomer(String id) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(Customer.class)
						.andEq("refereeId", id);
		
		return queryList(finderExpression);
	}

	@Override
	public int queryOneTeamShareCount(String id) {
		IFinderExpression oneCountFinder=FinderExpressionFactory.createExpression();
		oneCountFinder.select("count(id)")
					  .from(Customer.class)
					  .andEq("refereeId", id);
		
		return queryCount(oneCountFinder);
	}

	@Override
	public int queryTwoTeamShareCount(String id) {
		IFinderExpression twoChildIdFinder=FinderExpressionFactory.createExpression();
		twoChildIdFinder.select("id")
						.from(Customer.class)
						.andWhere("refereeId=#{refereeId}");
		
		IFinderExpression oneCountFinder=FinderExpressionFactory.createExpression();
		oneCountFinder.select("count(id)")
					  .from(Customer.class)
					  .andWhere("refereeId in("+twoChildIdFinder.toSql()+")")
					  .setParam("refereeId", id);
		
		return queryCount(oneCountFinder);
	}


	//用于后台的查询方法
	@Override
	public List<Customer> queryListMP(CustomerSearch customerSearch,
			PagerModel pagerModel) {
		return customerMapper.queryListMP(customerSearch, pagerModel);
	}
	

	@Override
	public List<Customer> queryListMP(CustomerSearch customerSearch) {
		return customerMapper.queryListMP(customerSearch, null);
	}

	@Override
	public int queryCountMP(CustomerSearch customerSearch) {
		return customerMapper.queryCountMP(customerSearch);
	}

	@Override
	public Customer queryModelMP(String key) {
		return customerMapper.queryModelMP(key);
	}
	
	@Override
	public List<Customer> queryListById(CustomerSearch customerSearch,
			PagerModel pagerModel,String key) {
		return customerMapper.queryListById(customerSearch, pagerModel,key);
	}

	@Override
	public int queryCountById(CustomerSearch customerSearch,String key) {
		return customerMapper.queryCountById(customerSearch,key);
	}

	
	@Override
	public boolean updateLevel(String openId,String level) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.update(Customer.class)
						.set("level=#{level}")
						.andWhere("openId=#{openId}")
						.setParam("level", level)
						.setParam("openId", openId);
		
		return update(finderExpression) > 0;
	}

	@Override
	public Customer queryRefereeModelByOpenId(String openId) {
		IFinderExpression childFinder = FinderExpressionFactory.createExpression();
		childFinder.select("c1.refereeId")
						.from(Customer.class,"c1")
						.andWhere("c1.openId=#{openId}");
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("c2.*")
						.from(Customer.class,"c2")
						.andWhere("id in ("+childFinder.toSql()+")")
						.setParam("openId", openId);
		
		return queryModel(finderExpression);
		
	}


	@Override
	public Customer queryModelByPhone(String phone) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(Customer.class)
						.andEq("phone", phone);
		
		return queryModel(finderExpression);
	}

	@Override
	public boolean updateCustomerByJoinMaker(Customer customer) {
		IFinderExpression updateExpression=FinderExpressionFactory.createExpression();
		updateExpression.update(Customer.class)
						.andEq("id", customer.getId());
		if(!StringUtils.isEmpty(customer.getUsername())){
			updateExpression.set("username=#{username}")
							.setParam("username", customer.getUsername());
		}
		if(!StringUtils.isEmpty(customer.getPhone())){
			updateExpression.set("phone=#{phone}")
							.setParam("phone", customer.getPhone());
		}
		if(!StringUtils.isEmpty(customer.getPassword())){
			updateExpression.set("password=#{password}")
							.setParam("password", customer.getPassword());
		}
		if(!StringUtils.isEmpty(customer.getUnionMark())){
			updateExpression.set("unionMark=#{unionMark}")
							.setParam("unionMark", customer.getUnionMark());
		}
		if(!StringUtils.isEmpty(customer.getRegion())){
			updateExpression.set("region=#{region}")
							.setParam("region", customer.getRegion());
		}
		if(!StringUtils.isEmpty(customer.getUpdateRegionDate())){
			updateExpression.set("updateRegionDate=#{updateRegionDate}")
							.setParam("updateRegionDate", customer.getUpdateRegionDate());
		}
		
		if(!StringUtils.isEmpty(customer.getRefereeId())){
			updateExpression.set("refereeId=#{refereeId}")
							.setParam("refereeId", customer.getRefereeId());
		}
		
		//第一个版本的时候是只有大于1000的时候是更新的，但是现在默认没有上级是0，是公司的是1，也是要更新的
		if(customer.getRefereeIdentity()>1000||customer.getRefereeIdentity()==1){
			updateExpression.set("refereeIdentity=#{refereeIdentity}")
							.setParam("refereeIdentity", customer.getRefereeIdentity());
		}
		if(!StringUtils.isEmpty(customer.getLevel())){
			updateExpression.set("level=#{level}")
							.setParam("level", customer.getLevel());
		}
		if(!StringUtils.isEmpty(customer.getJoinHighMakerDate())){
			updateExpression.set("joinHighMakerDate=#{joinHighMakerDate}")
							.setParam("joinHighMakerDate", customer.getJoinHighMakerDate());
		}
		if(!StringUtils.isEmpty(customer.getJoinMakerDate())){
			updateExpression.set("joinMakerDate=#{joinMakerDate}")
							.setParam("joinMakerDate", customer.getJoinMakerDate());
		}
		
		return update(updateExpression)>0;
	}

	@Override
	public List<Customer> queryListByBuyer(String loginId) {
		return customerMapper.queryListByBuyer(loginId);
	}
	
	@Override
	public boolean updateCustomerState(String loginId, String autonym,String username,String idType, String idCode) {
		IFinderExpression updateExpression=FinderExpressionFactory.createExpression();
		updateExpression.update(Customer.class)
			.set("id=#{id}")
			.setParam("id", loginId);
						
		if(!StringUtils.isEmpty(autonym)){			
			updateExpression.set("autonym=#{autonym}")
			.setParam("autonym", autonym);			
		}
		if (!StringUtils.isEmpty(idType)) {			
			updateExpression.set("idType=#{idType}")
			.setParam("idType", idType);			
		}
		if (!StringUtils.isEmpty(idCode)) {			
			updateExpression.set("idCode=#{idCode}")
			.setParam("idCode", idCode);			
		}
		updateExpression.andEq("id",loginId);
		return update(updateExpression)>0;
	}

	@Override
	public boolean updateCustDetail(String column,String id,Object value){
		
		IFinderExpression updateExpression=FinderExpressionFactory.createExpression();
		
		updateExpression.update(Customer.class)	
						.set(column+"=#{column}")
						.setParam("column", value)
						.andEq("id",id);
		
		return update(updateExpression)>0;
	}

	@Override
	public Boolean updateCustomerStateById(String id, String state) {

		IFinderExpression updateExpression=FinderExpressionFactory.createExpression();
		
		updateExpression.update(Customer.class)	
						.set("autonym"+"=#{autonym}")
						.setParam("autonym", state)
						.andEq("id",id);
		
		return update(updateExpression)>0;
	}

	@Override
	public List<TeamShareCustomer> queryWechatSearch(String searchContent,String id) {
		List<TeamShareCustomer>teamShareCustomer1 = null;
		List<TeamShareCustomer>teamShareCustomer2 = null;
		
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("id")
						.from(Customer.class)
						.andWhere("refereeId=#{refereeId}");
		
		IFinderExpression finderExpression1=FinderExpressionFactory.createExpression();
		finderExpression1.select("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath","count(c2.id) as personCount")
						.from(Customer.class,"c1")
						.leftJoin(Customer.class,"c2").on("c2.refereeId=c1.id")
						.andWhere("c1.refereeId=#{refereeId}")
						.setParam("refereeId", id)
						.andWhere("CONCAT_WS('_',c1.username,c1.nickname,c1.phone) like #{searchContent}")
						.setParam("searchContent", "%"+searchContent+"%")
						.addGroups("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath");
		teamShareCustomer1 = queryList(finderExpression1,TeamShareCustomer.class);
		
		for (int index = 0; index <teamShareCustomer1.size( ); index++) {
			System.err.println(teamShareCustomer1.get(index));
		}
		
		IFinderExpression finderExpression2=FinderExpressionFactory.createExpression();
		finderExpression2.select("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath","count(c2.id) as personCount")
						.from(Customer.class,"c1")
						.leftJoin(Customer.class,"c2").on("c2.refereeId=c1.id")
						.andWhere("c1.refereeId in("+oneChildIdFinder.toSql()+")")
						.setParam("refereeId", id)
						.andWhere("CONCAT_WS('_',c1.username,c1.nickname,c1.phone) like #{searchContent}")
						.setParam("searchContent", "%"+searchContent+"%")
						.addGroups("c1.userIdentity","c1.username","c1.nickname","c1.id","c1.level","c1.picPath");
		teamShareCustomer2 = queryList(finderExpression2,TeamShareCustomer.class);
		
		for (int i = 0; i < teamShareCustomer2.size(); i++) {
			teamShareCustomer1.add(teamShareCustomer2.get(i));
		}

		return teamShareCustomer1;
	}

	@Override
	public List<Customer> queryListForAward(Date startDate, Date endDate) {
		return customerMapper.queryListForAward(startDate, endDate);
	}

	@Override
	public int queryCustCount(String year,String month,String day,String nativeCity) {
		
		return customerMapper.queryCustCount(year,month,day,nativeCity);
	}

	@Override
	public int queryAllCustCount(String date,String nativeCity) {
		
		return customerMapper.queryAllCustCount(date,nativeCity);
	}

	@Override
	public Boolean updatePassword(String newPassword, String customerId,String unionMark) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.update(Customer.class)
						.set("password=#{newPassword}")
						.set("unionMark=#{unionMark}")
						.andWhere("id=#{customerId}")
						.setParam("newPassword", newPassword)
						.setParam("unionMark", unionMark)
						.setParam("customerId", customerId);
		
		return update(finderExpression) > 0;

	}

	@Override
	public List<CustomerTree> queryTopList(CustomerSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
						.from(Customer.class)
						.andWhere("refereeId = #{id}")
						.setParam("id", search.getId())
						.addOrder("userIdentity",OrderEnum.asc);
						
						
		return queryList(finderExpression, CustomerTree.class);
	}

	@Override
	public List<CustomerTree> queryTreeList() {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
						.from(Customer.class)
						.addOrder("userIdentity",OrderEnum.asc);
						
						
		return queryList(finderExpression, CustomerTree.class);
	}

	@Override
	public CustomerTree queryModelForConsumption(CustomerSearch search) {
		IFinderExpression notCarExpression=FinderExpressionFactory.createExpression();
		
		notCarExpression.select("sum(b1.brokerage)")
						.from(BrokerageRecord.class,"b1")
						.andWhere("b1.userId = customer.id");
		
        IFinderExpression carExpression=FinderExpressionFactory.createExpression();
		
        carExpression.select("sum(b2.bizInsureInfo)")
					 .from(CarBrokerageRecord.class,"b2")
					 .andWhere("b2.customerId = customer.id");
        
        
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("customer.*","(" + notCarExpression.toSql() + ") as notCar", "(" + carExpression.toSql() + ") as car")
		             	.from(Customer.class,"customer")
		             	.andWhere("customer.id = #{key}")
		             	.setParam("key", search.getId());
		           
						
		return queryModel(finderExpression, CustomerTree.class);
	}
	


	@Override
	public List<CustomerTree> queryListForConsumption(CustomerSearch search) {
		IFinderExpression notCarExpression=FinderExpressionFactory.createExpression();
		
		notCarExpression.select("sum(b1.brokerage)")
						.from(BrokerageRecord.class,"b1")
						.andWhere("b1.userId = customer.id");
		
        IFinderExpression carExpression=FinderExpressionFactory.createExpression();
		
        carExpression.select("sum(b2.bizInsureInfo)")
					 .from(CarBrokerageRecord.class,"b2")
					 .andWhere("b2.customerId = customer.id");
        
        IFinderExpression idsExpression=FinderExpressionFactory.createExpression();
		
        idsExpression.select("c2.id")
					 .from(Customer.class,"c2")
					 .andWhere("c2.refereeId = #{key}");
        
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("customer.*","(" + notCarExpression.toSql() + ") as notCar","(" + carExpression.toSql() + ") as car")
		             	.from(Customer.class,"customer")
		             	.andWhere("customer.id in (" + idsExpression.toSql() + ")")
		             	.setParam("key", search.getRefereeId());
						
		return queryList(finderExpression, CustomerTree.class);
	}

	@Override
	public List<Customer> queryCountForStatistic(String state,CustomerSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
						.from(Customer.class);
		
		appendForStatistic(state,finderExpression,search);
		
		return queryList(finderExpression);
	}

	private void appendForStatistic(String state,IFinderExpression appendForStatistic,CustomerSearch search) {
		if("attentionDate".equals(state)){
			appendForStatistic.andWhere("DATE_FORMAT( `attentionDate`, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
		}else if("joinMakerDate".equals(state)){
			appendForStatistic.andWhere("DATE_FORMAT( `joinMakerDate`, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
		}else if("joinHighMakerDate".equals(state)){
			appendForStatistic.andWhere("DATE_FORMAT( `joinHighMakerDate`, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
		}
		
		
		if(!StringUtils.isEmpty(search.getCityId())){
			appendForStatistic.andWhere("cityId = #{cityId}")
							  .setParam("cityId",search.getCityId());
		}
		
		if(!StringUtils.isEmpty(search.getRefereeId())){
			appendForStatistic.andWhere("refereeId = #{refereeId}")
			  				  .setParam("refereeId",search.getRefereeId());
		}
	}

	@Override
	public CustomerStatistic queryModel(CustomerSearch search) {
		//高创数
		IFinderExpression highMakerExpression=FinderExpressionFactory.createExpression();
		
		highMakerExpression.select("COUNT(*)")
						.from(Customer.class)
						.andWhere("level = #{highMaker}");
		
		appendFinder(search,highMakerExpression);
		
		//初创数
		IFinderExpression makerExpression=FinderExpressionFactory.createExpression();
		
		 makerExpression.select("COUNT(*)")
						.from(Customer.class)
						.andWhere("level = #{maker}");
		 
		 appendFinder(search,makerExpression);
		 
		//会员数
		 IFinderExpression vipExpression=FinderExpressionFactory.createExpression();
			
		  vipExpression.select("COUNT(*)")
						.from(Customer.class)
						.andWhere("level = #{vip}");
		  
		  appendFinder(search,vipExpression);
		  
		  //游客数
		  IFinderExpression visitorExpression=FinderExpressionFactory.createExpression();
			
		  visitorExpression.select("COUNT(*)")
						   .from(Customer.class)
						   .andWhere("level = #{visitor}");
		  
		  appendFinder(search,visitorExpression);
		  
		  //整合
		  IFinderExpression fiderExpression=FinderExpressionFactory.createExpression();
		  
		  fiderExpression.select("count(*) as total","(" + highMakerExpression.toSql() + ") as highMaker","(" + makerExpression.toSql() + ") as maker","(" + vipExpression.toSql() + ") as vip","(" + visitorExpression.toSql() + ") as visitor")
		  				 .from(Customer.class)
		  				 .setParam("visitor", CustomerLevel.visitor)
		  				 .setParam("vip", CustomerLevel.vip)
		  				 .setParam("maker", CustomerLevel.maker)
		  				 .setParam("highMaker", CustomerLevel.highMaker);
		  
		  appendFinder(search,fiderExpression);
		
		  return queryModel(fiderExpression, CustomerStatistic.class);
	}

	private void appendFinder(CustomerSearch search, IFinderExpression finderExpression) {
		if(!StringUtils.isEmpty(search.getCityId())){
			finderExpression.andEq("cityId", search.getCityId());
		}
	}

	/*<----------------------------------API------------------------------------>*/
	@Override
	public Customer login(String userId, String password) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.addBody("select * from " + getTableName() + " customer")
						.andWhere("customer.phone=#{userId} or customer.userIdentity=#{userId}")
						.setParam("userId", userId);
		
		List<Customer> list = queryList(finderExpression);
		
		if(list == null || list.size() <=0){
			return null;
		}
		
		for (Customer customer : list){
			String customerPass = DESedeCoder.decrypt(customer.getLoginPassword());
			if(password.equals(customerPass)){
				return customer;
			}
		}
		return null;
	}

	@Override
	public Boolean queryUnionId(String unionId) {
		IFinderExpression finderExpression  = FinderExpressionFactory.createExpression();

		finderExpression.select("count(id)")
		  .from(Customer.class)
		  .andWhere("unionId = #{unionId}")
		  .setParam("unionId", unionId);
		
		return queryCount(finderExpression)>0;
	}

	@Override
	public Customer queryModelByUnionId(String unionId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
						.from(Customer.class)
						.andWhere("unionId = #{unionId}")
						.setParam("unionId", unionId);
		
		return queryModel(finderExpression);
	}

	@Override
	public Customer queryModelByUnionIdOrPhone(String unionId, String phone) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
						.from(Customer.class)
						.andWhere("unionId = #{unionId}")
						.orWhere("phone = #{phone}")
						.setParam("unionId", unionId)
						.setParam("phone",phone);
		
		return queryModel(finderExpression);
	}

	@Override
	public boolean updateCustomerColumn(CustomerColumn customerColumn) {
		IFinderExpression updateExpression=FinderExpressionFactory.createExpression();
		
		updateExpression.update(Customer.class)
						.set("level=#{level}")
						.setParam("level", customerColumn.getLevel())
						.set("region=#{region}")
						.setParam("region", customerColumn.getRegion());
						
		if(!StringUtils.isEmpty(customerColumn.getPhone())){			
			updateExpression.set("phone=#{phone}")
							.setParam("phone",customerColumn.getPhone());			
		}
		if (!StringUtils.isEmpty(customerColumn.getUsername())) {			
			updateExpression.set("username=#{username}")
							.setParam("username", customerColumn.getUsername());			
		}
		if (customerColumn.getJoinMakerDate() != null) {			
			updateExpression.set("joinMakerDate=#{joinMakerDate}")
							.setParam("joinMakerDate", customerColumn.getJoinMakerDate());			
		}
		if (customerColumn.getJoinHighMakerDate() != null) {			
			updateExpression.set("joinHighMakerDate=#{joinHighMakerDate}")
							.setParam("joinHighMakerDate", customerColumn.getJoinHighMakerDate());			
		}
		
		if (!StringUtils.isEmpty(customerColumn.getUpdateRegionDate())) {			
			updateExpression.set("updateRegionDate=#{updateRegionDate}")
							.setParam("updateRegionDate", customerColumn.getUpdateRegionDate());			
		}
		
		if(!StringUtils.isEmpty(customerColumn.getRefereeIdentity())){
			updateExpression.set("refereeIdentity=#{refereeIdentity}")
							.setParam("refereeIdentity", Integer.valueOf(customerColumn.getRefereeIdentity()))
							.set("refereeId=#{refereeId}")
							.setParam("refereeId", customerColumn.getRefereeId());
		}
		
		updateExpression.andWhere("userIdentity = #{userIdentity}")
						.setParam("userIdentity", Integer.valueOf(customerColumn.getUserIdentity()));
		
		return update(updateExpression)>0;
	}

	@Override
	public int queryCountDayMakers(String refereeId) {
		return customerMapper.queryCountDayMakers(refereeId);
	}

	@Override
	public int queryCountWeekMakers(String refereeId) {
		return customerMapper.queryCountWeekMakers(refereeId);
	}

	@Override
	public int queryCountMonthMakers(String refereeId) {
		return customerMapper.queryCountMonthMakers(refereeId);
	}

	@Override
	public List<Customer> queryNextVisitors(String refereeId) {
		return customerMapper.queryNextVisitors(refereeId);
	}
		
}
